<html>
<head>
    <title>Top Down Operator Precedence</title>
    <style>
		th {background-color: thistle; border: black solid 1px; text-align: left; padding-left: 10px; padding-right: 10px; vertical-align: top;}
		td {background-color: white; border: black solid 1pt; padding-left: 10px; padding-right: 10px; vertical-align: top;}
		table {width: 90%; border: 0px; cellpadding: 0px;}
    </style>
</head>

<body bgcolor=linen>
<h1>Top Down Operator Precedence</h1>
<!-- 
<p><a href="http://www.crockford.com/">Douglas Crockford</a></p>

<p>2007-08-05</p>

<h2>Demonstration</h2>
<p>This is the companion file to my paper on <a href="http://javascript.crockford.com/tdop/tdop.html">Vaughan
  Pratt's Top Down Operator Precedence</a>. The paper presents a parser
  for Simplified JavaScript that is written in Simplified Javascript. It
  is chapter 9 of <a href="http://www.amazon.com/exec/obidos/ASIN/0596510047/wrrrldwideweb">Beautiful
  Code</a>. This page loads that parser and runs the parser on itself. </p>
<p>This page loads three JavaScript files:</p>
<table border="0" align="center">
  <tr>
    <td valign="top"><a href="tokens.js">tokens.js</a></td>

    <td>This file installs the <code>String.prototype.tokens</code> method. It
      produces an array of simple tokens from a string.</td>
  </tr>
  <tr>
    <td valign="top"><a href="parse.js">parse.js</a></td>
    <td>This file installs the <code>make_parse</code> function which produces
      the parse function. The indirection was done so that we could conveniently
      recover the source of the function.</td>

  </tr>
  <tr>
    <td valign="top"><a href="http://json.org/json2.js">json2.js</a></td>
    <td>This file installs the <code>JSON.stringify</code> method.
      It is used to display the parse tree.</td>
  </tr>
</table>
<p>The text that follows is the parse tree that the parser generated by
  parsing itself. </p> -->

<noscript><p>You may not see it because you have JavaScript turned off. Uffff!</p></noscript>
<script src="tokens.js"></script>
<script src="parse.js"></script>
<script src="json2.js"></script>
<pre>
<script>
/*jslint evil: true */

/*members create, error, message, name, prototype, stringify, toSource,
    toString, write
*/

/*global JSON, make_parse, parse, source, tree */

// Make a new object that inherits members from an existing object.

if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}

// Transform a token object into an exception object and throw it.

Object.prototype.error = function (message, t) {
    t = t || this;
    t.name = "SyntaxError";
    t.message = message;
    throw t;
};


try {
    parse = make_parse();

// We are going to make the parse function parse itself.

    source = make_parse.toSource ?
            make_parse.toSource() : make_parse.toString();
    source = "var make_parse = " + source + ";";
    tree = parse(source);
    if (tree) {
        document.write(JSON.stringify(tree, ['key', 'name', 'message',
            'value', 'arity', 'first', 'second', 'third', 'fourth'], 4));
    }

} catch (e) {
    document.write(JSON.stringify(e, ['name', 'message', 'from', 'to', 'key',
            'value', 'arity', 'first', 'second', 'third', 'fourth'], 4));
}

</script>
</pre>
</body>
</html>
